package com.kingint.web.admin.serve.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kingint.mbg.dto.CheckRoom.CheckRoomQueryParam;
import com.kingint.mbg.ser.entity.SerCheckRoom;
import com.kingint.mbg.ser.entity.SerReceiveWorkMiddle;
import com.kingint.mbg.ser.entity.SerWorkOrder;
import com.kingint.mbg.ser.mapper.SerCheckRoomMapper;
import com.kingint.mbg.ser.mapper.SerReceiveWorkMiddleMapper;
import com.kingint.mbg.ser.mapper.SerWorkOrderMapper;
import com.kingint.mbg.sys.entity.UmsMember;
import com.kingint.web.admin.serve.service.SerCheckRoomService;
import com.kingint.web.admin.serve.service.SerReceiveWorkMiddleService;
import com.kingint.web.portal.serve.service.ActivitiCancelService;
import com.kingint.web.portal.serve.service.CheckRoomService;
import com.kingint.web.sys.service.UmsAdminService;
import com.kingint.web.sys.service.UmsMemberService;
import com.kingint.web.util.CharUtil;
import com.kingint.web.util.WorkType;
import com.kingint.web.util.WorkTypeUtil;
import com.kingint.web.vo.CheckRoomBean;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.HistoryService;
import org.activiti.engine.TaskService;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 查房工单详情表 服务实现类
 * </p>
 *
 * @author wxh
 * @since 2020-12-24
 */
@Slf4j
@Service
public class SerCheckRoomServiceImpl extends ServiceImpl<SerCheckRoomMapper, SerCheckRoom> implements SerCheckRoomService {

    @Resource
    private UmsAdminService adminService;
    @Resource
    private UmsMemberService umsMemberService;
    @Resource
    private SerWorkOrderMapper workOrderMapper;
    @Resource
    private SerReceiveWorkMiddleMapper serReceiveWorkMiddleMapper;
    @Resource
    private SerCheckRoomMapper serCheckRoomMapper;
    @Resource
    private ActivitiCancelService activitiCancelService;
    @Resource
    private SerReceiveWorkMiddleService serReceiveWorkMiddleService;
    @Resource
    private TaskService taskService;
    @Resource
    private HistoryService historyService;

    private String repairInstanceKey = CheckRoomBean.PROCESSINSTANCEKEY;

    /**
     * 创建并完成查房工单
     * @param serCheckRoom
     * @return
     */
    @Override
    public boolean addCheckRoomOder(SerCheckRoom serCheckRoom) {
        UmsMember currentMember = adminService.getCurrentMember();//当前用户
        //添加总工单信息
        SerWorkOrder serWorkOrder = new SerWorkOrder();
        BeanUtils.copyProperties(serCheckRoom, serWorkOrder);
        serWorkOrder.setRoomId(serCheckRoom.getRoomId());
        serWorkOrder.setSubmitUserId(currentMember.getId());//提交人员
        serWorkOrder.setCreateTime(new Date());
        serWorkOrder.setWorkOrderStatus(0);
        serWorkOrder.setOrderStatus(6);
        serWorkOrder.setHotelId(currentMember.getHotelId());
        serWorkOrder.setSubmitUserId(currentMember.getId());
        serWorkOrder.setRoomNum(serCheckRoom.getRoomNum());
        serWorkOrder.setOrderSn(CharUtil.generateOrderSn()); //工单号
        serWorkOrder.setWorkTypeId(WorkTypeUtil.getWorkTypeIdByName(WorkType.checkRoomName)); //工单类型
        //存入总工单
        int insert = workOrderMapper.insert(serWorkOrder);
        SerReceiveWorkMiddle receiveWorkMiddle = new SerReceiveWorkMiddle();
        receiveWorkMiddle.setReceiveUserId(serCheckRoom.getUserId().longValue());
        receiveWorkMiddle.setWorkId(serWorkOrder.getId());
        receiveWorkMiddle.setCreateTime(new Date());
        int insert2 = serReceiveWorkMiddleMapper.insert(receiveWorkMiddle);
        String businessKey = "";
        if (insert > 0){
            businessKey = CheckRoomBean.PROCESSINSTANCEKEY + "_" + currentMember.getHotelId() + "_" + serWorkOrder.getId();

            //创建查房表信息
            serCheckRoom.setWorkId(serWorkOrder.getId());
            int insert1 = serCheckRoomMapper.insert(serCheckRoom);
            if (insert1 > 0){
                HashMap<String,Object> checkroomuser = new HashMap<>();
                checkroomuser.put(CheckRoomBean.CHECKPEOPLE,serCheckRoom.getUserName());

                String processInstanceId = activitiCancelService.startProcess(CheckRoomBean.PROCESSINSTANCEKEY, businessKey, checkroomuser);
                serWorkOrder.setProcessInstanceId(processInstanceId);
                int i = workOrderMapper.updateById(serWorkOrder);
                return true;
            }

        }
        return false;
    }

    /**
     * 获取所有查房工单
     * @return
     */
    @Override
    public IPage<SerCheckRoom> getCheckRoomOder(CheckRoomQueryParam serCheckRoom, Integer pageNum, Integer pageSize) {
        UmsMember currentMember = adminService.getCurrentMember();
        Integer hotelId = currentMember.getHotelId();//对应的酒店id
        if (StringUtils.isEmpty(hotelId)) {
            log.debug("当前用户账号没有关联酒店id");
            return null;
        }
        serCheckRoom.setHotelId(hotelId);
        //获取工单类型id
        Integer workTypeId = WorkTypeUtil.getWorkTypeIdByName(WorkType.checkRoomName);
        IPage<SerCheckRoom> page = new Page<>(pageNum,pageSize);
        IPage<SerCheckRoom> checkRoomIPage = serCheckRoomMapper.getListPage(page,serCheckRoom);
        return checkRoomIPage;
    }

    @Override
    public boolean complete(SerCheckRoom serCheckRoom) {
        UmsMember currentMember = adminService.getCurrentMember();//当前用户
        String businessKey = CheckRoomBean.PROCESSINSTANCEKEY + "_" + currentMember.getHotelId() + "_" + serCheckRoom.getWorkId();
            //查询运行时的信息
            Task task = taskService.createTaskQuery()
                    .processDefinitionKey(CheckRoomBean.PROCESSINSTANCEKEY)
                    .processInstanceBusinessKey(businessKey)
                    .singleResult();
            //结束流程
            taskService.complete(task.getId());
        SerWorkOrder serWorkOrder = workOrderMapper.selectById(serCheckRoom.getWorkId());
        serWorkOrder.setEndTime(new Date());
            int i = workOrderMapper.updateById(serWorkOrder);
            return i > 0 ? true : false;
    }

    /**
     * 根据id查询
     * @param id 查房工单表id
     * @return
     */
    @Override
    public SerCheckRoom getonebyid(Integer id) {
        SerCheckRoom serCheckRoom = serCheckRoomMapper.selectById(id);
        SerWorkOrder serWorkOrder = workOrderMapper.selectById(serCheckRoom.getWorkId());
        QueryWrapper<SerReceiveWorkMiddle> middleQueryWrapper = new QueryWrapper<>();
        middleQueryWrapper.eq("work_id",serWorkOrder.getId());
        SerReceiveWorkMiddle one = serReceiveWorkMiddleService.getOne(middleQueryWrapper);
        serCheckRoom.setUserName(umsMemberService.getById(one.getReceiveUserId()).getUsername());
        serCheckRoom.setSerWorkOrder(serWorkOrder);
        return serCheckRoom;
    }
}
